package b6.vacancyshare.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBPengguna
{
	public static final String KEY_ROWIDPENGGUNA = "rowidpengguna";
    public static final String KEY_USERNAME = "username";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_EMAIL = "email";

    private static final String TAG = "DBPengguna";

    private static final String DATABASE_NAME = "vacancy-share";
    private static final String DATABASE_TABLE = "pengguna";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table PENGGUNA (rowidpengguna integer autoincrement, "
    	+ "username integer not null, "
        + "password text not null, "
        + "email text not null,"
        + "PRIMARY KEY(rowidpengguna,username));";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBPengguna(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    public boolean Login(String username, String password) throws SQLException 
    {
    	Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
        if (mCursor != null) {           
            if(mCursor.getCount() > 0)
            {
            	return true;
            }
        }
     return false;
    }
    
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,
        int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS pengguna");
            onCreate(db);
        }
    }

    //---opens the database---
    public DBPengguna open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---
    public void close()
    {
        DBHelper.close();
    }

    //---insert a Pengguna into the database---
    public long insertPengguna(String username, String password, String email)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_USERNAME, username);
        initialValues.put(KEY_PASSWORD, password);
        initialValues.put(KEY_EMAIL, email);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

  //---deletes a particular title---
    public boolean deletePengguna(long rowidpengguna)
    {
        return db.delete(DATABASE_TABLE, KEY_ROWIDPENGGUNA +
        		"=" + rowidpengguna, null) > 0;
    }

  //---retrieves all the pengguna---
    public Cursor getAllPengguna()
    {
        return db.query(DATABASE_TABLE, new String[] {
        		KEY_ROWIDPENGGUNA,
        		KEY_USERNAME,
        		KEY_PASSWORD,
                KEY_EMAIL},
                null,
                null,
                null,
                null,
                null);
    }

  //---retrieves a particular pengguna---
    public Cursor getPengguna(long rowidpengguna) throws SQLException
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                		KEY_ROWIDPENGGUNA,
                		KEY_USERNAME,
                		KEY_PASSWORD,
                		KEY_EMAIL
                		},
                		KEY_ROWIDPENGGUNA + "=" + rowidpengguna,
                		null,
                		null,
                		null,
                		null,
                		null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

  //---updates a Pengguna---
    public boolean updatePengguna(long rowidpengguna, String username,
    String password, String email)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_USERNAME, username);
        args.put(KEY_PASSWORD, password);
        args.put(KEY_EMAIL, email);
        return db.update(DATABASE_TABLE, args,
                         KEY_ROWIDPENGGUNA + "=" + rowidpengguna, null) > 0;
    }



}
